<div class="layuimini-main" id="DeviceReport-search">


    <table class="layui-hide" id="PeoplePage" lay-filter="PeoplePageTableFilter"></table>
    <div class="layui-row layui-col-space15">
        <div class="layui-panel defcenter">
            <button type="submit" class="layui-btn  data-add-btn" lay-submit lay-filter="data-get-btn" id="GetPeopls"><i class="layui-icon">&#xe605;</i> 获取所有人员</button>
            <button type="submit" class="layui-btn  data-add-btn" lay-submit lay-filter="data-save-btn" id="SaveSelectPeoples"><i class="layui-icon">&#xe605;</i> 保存选中</button>
            <button type="submit" class="layui-btn  data-add-btn" lay-submit lay-filter="data-save-btn" id="SaveAllPeoples"><i class="layui-icon">&#xe605;</i> 保存所有</button>
            <button type="submit" class="layui-btn layui-btn-danger  data-delete-btn" id="btnEditClose"><i class="layui-icon">&#x1006;</i> 取消</button>
        </div>
    </div>
    <div class="layui-row layui-col-space15">
        <div style="" id="show">
            <div class="layui-progress layui-progress-big" lay-filter="demo" lay-showPercent="true">
                <div class="layui-progress-bar" lay-percent="0%"></div>
            </div>
        </div>
    </div>
</div>
<script>

    layui.use(['form', 'upload', 'element', 'layer', 'faceAPI'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
        var upload = layui.upload
            , element = layui.element
            , layer = layui.layer,
            table = layui.table;
        var slider = layui.slider;
        var faceAPI = layui.faceAPI;
        var laydate = layui.laydate;
        var deviceSn = faceAPI.GetDevicePoeple.Code;
        var parentIndex = layer.index;
        $("#btnEditClose").click(function () {
            layer.close(parentIndex);
        });
        LoadPage(null);

        function LoadPage(data) {
            if (data == null) {
                data = [];
            }
            table.render({
                elem: '#PeoplePage',
                //**************************异步请求开始***********************
                // url: 'api/PeopleNew/GetDevicePeopleAll/' + deviceSn,
                method: 'post',
                headers: {
                    // token: 'sasasas',
                    Authorization: "Bearer " + faceAPI.GetJWTToken()
                },//增加Token验证头
                request: {
                    pageName: 'pageIndex', //页码的参数名称，默认：page
                    limitName: 'pageSize' //每页数据量的参数名，默认：limit
                },
                parseData: function (res) { //res 即为原始返回的数据
                    if (res.errCode == 0) {
                        var data = res.content;
                        var ret = {
                            "code": res.errCode, //解析接口状态
                            "msg": res.error, //解析提示文本
                            "count": data.totalCount, //解析数据长度
                            "data": data.dataList //解析数据列表
                        };
                        return ret;

                    } else {
                        /*layer.open({
                            title: '请求错误',
                            content: '(' + res.errCode + ')' + res.error
                        });  */

                        return {
                            "code": 0, //解析接口状态
                            "msg": '', //解析提示文本
                            "count": 0, //解析数据长度
                            "data": [] //解析数据列表
                        };
                    }

                },
                //**************************异步请求结束***********************

                //toolbar: '#toolbarDemo',
                //defaultToolbar: ['filter', 'exports', 'print', {
                //    title: '提示',
                //    layEvent: 'LAYTABLE_TIPS',
                //    icon: 'layui-icon-tips'
                //}],
                cols: [[
                    { type: "checkbox", width: 50 },
                    // { field: 'id', width: 70, title: 'ID', sort: true },
                    { field: 'code', width: 140, title: '人员编号', sort: true },
                    { field: 'name', width: 160, title: '姓名', sort: true },
                    {//人员识别凭证
                        field: 'card', width: 100, title: '凭证', sort: false,
                        templet: function (d) {
                            //    debugger
                            var sBuf = [];
                            if (d.hasOwnProperty("card"))
                                if (d.card > 0) {
                                    sBuf.push('<i class="PeopleAccessKey fa fa-credit-card"  typeTitle="卡号"  type="text"  value = "' + d.card + '"></i>');
                                }
                            //if (d.hasOwnProperty("photo"))
                            //    if (d.photo.length > 0) {
                            //        sBuf.push('<i class="PeopleAccessKey fa fa-child" typeTitle="照片" type="image" value = "' + d.photo + '"></i>');
                            //    }
                            if (d.hasOwnProperty("pin"))
                                if (d.pin.length > 0) {
                                    sBuf.push('<i class="PeopleAccessKey fa fa-at" typeTitle="密码" type="text" value = "' + d.pin + '"></i>');
                                }

                            return sBuf.join(' ');
                        }
                    },
                    {
                        field: 'admin', title: '是否有头像', width: 160, sort: true,
                        templet: function (d) {
                            var type = "有头像";
                            if (d.featureData == null || d.featureData.length == 0)
                                type = "无头像";
                            return type;
                        }
                    },
                    { field: 'job', title: '职务', width: 160, sort: true },
                    {
                        field: 'admin', title: '人员类型', width: 160, sort: true,
                        templet: function (d) {
                            var type = "管理人员";
                            if (d.admin == 0)
                                type = "普通用户";
                            return type;
                        }
                    },
                    {
                        field: 'phone', title: '手机号', width: 160, sort: true,
                        templet: function (d) {
                          //  debugger
                            if (d.attachment != null && d.attachment.length > 0)
                                return attachment['phone'];
                            return "";
                        }
                    },
                    {
                        field: 'identityId', title: '身份证号码', width: 160, sort: true,
                        templet: function (d) {
                           // debugger
                            if (d.attachment != null && d.attachment.length > 0)
                                return attachment['identityId'];
                            return "";
                        }
                    },

                    //  { title: '操作', minWidth: 150, toolbar: '#GetPeoplesCurrentTableBar', align: "center" }
                ]],
                limits: [10, 50, 100, 200, 400, 600],
                limit: 10,
                page: true,
                skin: 'line',
                done: function (res, curr, count) {
                    //绑定事件
                    if (res.count > 0) {
                        //lay-filter="PeoplePageTableFilter"
                        // bindPeopleAccessKey();
                    }
                },
                data: data
            });
        }
        var PeopleList = null;
        $("#GetPeopls").click(function () {
           
            var index = layer.load(2, {
                shade: [0.8, '#130c0e'], //0.1透明度的白色背景
                content: '正在获取人员操作',
                success: function (layero) {
                    layero.find('.layui-layer-content').css({
                        'paddingTop': '80px',
                        'width': '260px',
                        'textAlign': 'center',
                        'backgroundPositionX': 'center',
                        'font-size': '20px',
                        'color': '#ef5b9c'
                    });
                }
            });
            faceAPI.Ajax({
                type: 'Post',
               
                url: 'api/PeopleNew/GetDevicePeopleAll/' + deviceSn,
                success: function (result) {
                  //  debugger
                    if (result.result == false) {
                        layer.alert(result.error, { icon: 2 });
                    }
                    else {
                        //var ret = {
                        //    "code": result.errCode, //解析接口状态
                        //    "msg": result.error, //解析提示文本
                        //    "count": result.content.totalCount, //解析数据长度
                        //    "data": result.content.dataList //解析数据列表
                        //};
                        PeopleList = result.content.dataList;
                        LoadPage(PeopleList);
                        layer.close(index);
                    }
                    // layer.close(index);
                }
            });

        });

        //保存选中
        $("#SaveSelectPeoples").click(function () {
            debugger
            var checkStatus = table.checkStatus('PeoplePage')
                , data = checkStatus.data;
            if (data.length == 0) {
                layer.alert("没有需要保存的人员数据", { icon: 1 });
                return;
            }
            var codes = [];
            data.forEach(function (item, index) { codes.push(item.code); });
            SavePeoples(codes);
        });
        $("#SaveAllPeoples").click(function () {
            debugger;
            if (PeopleList == null || PeopleList.length==0) {
                layer.alert("没有需要保存的人员数据", { icon: 1 });
                return;
            }
            var codes = [];
            PeopleList.forEach(function (item, index) { codes.push(item.code); });
            SavePeoples(codes);
        });
        function SavePeoples(codes) {
          

            faceAPI.Ajax({
                type: 'Post',
                data: { codes: codes, deviceSn: deviceSn },
                url: '/api/Background/StartGetAllPersonTask',
                success: function (result) {

                    if (result.result == true) {
                        layer.alert(result.content, { icon: 1 });
                    }
                }
            });
        };


        function GetProcessStep() {
            fun = setInterval(function () {

                faceAPI.Ajax({
                    type: 'Post',
                    url: '/api/Background/GetProcessStep',
                    success: function (result) {
                        if (result.result == true) {

                            if (result.content.processStep >= result.content.processMax) {
                                clearInterval(fun);
                            }
                            //debugger
                            element.progress('demo', result.content.processStep + '/' + result.content.processMax);
                          
                        }
                    }
                });
            }, 100);
        }
    });

</script>
